package com.artisan.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.artisan.dao.TeacherDao;
import com.artisan.model.Teacher;
import com.artisan.util.StringUtil;

import javax.swing.JButton;
import javax.swing.JFrame;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;

public class AddTeacherFrm extends JInternalFrame {
	private JTextField teacherNameTextField;
	private JTextField teacherTitleTextField;
	private JTextField teacherAgeTextField;
	private JRadioButton teacherSexManRadioButton;
	private JRadioButton teacherSexFemaleRadioButton;
	private JPasswordField teacherPasswordField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddTeacherFrm frame = new AddTeacherFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddTeacherFrm() {
		setFrameIcon(new ImageIcon(AddTeacherFrm.class.getResource("/com/artisan/image/老师.png")));
		setClosable(true);
		setIconifiable(true);

		getContentPane().setFont(new Font("微软雅黑", Font.PLAIN, 14));
		setTitle("添加教师");
		setBounds(100, 100, 677, 471);

		JLabel lblNewLabel = new JLabel("教师姓名:");
		lblNewLabel.setIcon(new ImageIcon(AddTeacherFrm.class.getResource("/com/artisan/image/老师.png")));
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		teacherNameTextField = new JTextField();
		teacherNameTextField.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("教师性别:");
		lblNewLabel_1.setIcon(new ImageIcon(AddTeacherFrm.class.getResource("/com/artisan/image/性别.png")));
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		ButtonGroup buttonGroup = new ButtonGroup();
		teacherSexManRadioButton = new JRadioButton("男");
		teacherSexManRadioButton.setSelected(true);
		teacherSexManRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		teacherSexFemaleRadioButton = new JRadioButton("女");
		teacherSexFemaleRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		buttonGroup.add(teacherSexManRadioButton);
		buttonGroup.add(teacherSexFemaleRadioButton);

		JLabel lblNewLabel_2 = new JLabel("教师职称:");
		lblNewLabel_2.setIcon(new ImageIcon(AddTeacherFrm.class.getResource("/com/artisan/image/职称评定.png")));
		lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		teacherTitleTextField = new JTextField();
		teacherTitleTextField.setColumns(10);

		JLabel lblNewLabel_3 = new JLabel("教师年龄:");
		lblNewLabel_3.setIcon(new ImageIcon(AddTeacherFrm.class.getResource("/com/artisan/image/年龄.png")));
		lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		teacherAgeTextField = new JTextField();
		teacherAgeTextField.setColumns(10);

		JButton submitButton = new JButton("确认添加");
		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				addTeacherAct(ae);
			}
		});
		submitButton.setIcon(new ImageIcon(AddTeacherFrm.class.getResource("/com/artisan/image/确认.png")));
		submitButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		JButton resetButton = new JButton("重置表单");
		resetButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				resetValue(ae);
			}
		});
		resetButton.setIcon(new ImageIcon(AddTeacherFrm.class.getResource("/com/artisan/image/重置.png")));
		resetButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		JLabel lblNewLabel_4 = new JLabel("登陆密码:");
		lblNewLabel_4.setIcon(new ImageIcon(AddTeacherFrm.class.getResource("/com/artisan/image/密码.png")));
		lblNewLabel_4.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		teacherPasswordField = new JPasswordField();
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.TRAILING).addGroup(groupLayout
				.createSequentialGroup().addGap(144)
				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel_3, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
						.addComponent(lblNewLabel_2, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
										.addComponent(lblNewLabel_1, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE,
												GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(lblNewLabel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 89,
												Short.MAX_VALUE))
								.addPreferredGap(ComponentPlacement.RELATED, 4, Short.MAX_VALUE))
						.addComponent(lblNewLabel_4))
				.addPreferredGap(ComponentPlacement.RELATED)
				.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING).addGroup(Alignment.LEADING,
						groupLayout.createSequentialGroup().addComponent(teacherSexManRadioButton)
								.addPreferredGap(ComponentPlacement.RELATED).addComponent(teacherSexFemaleRadioButton,
										GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE))
						.addComponent(teacherPasswordField, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 253,
								Short.MAX_VALUE)
						.addGroup(Alignment.LEADING,
								groupLayout.createParallelGroup(Alignment.TRAILING, false)
										.addComponent(teacherAgeTextField, Alignment.LEADING)
										.addComponent(teacherTitleTextField, Alignment.LEADING)
										.addComponent(teacherNameTextField, Alignment.LEADING, GroupLayout.DEFAULT_SIZE,
												253, Short.MAX_VALUE))
						.addGroup(Alignment.LEADING,
								groupLayout.createSequentialGroup().addComponent(submitButton)
										.addPreferredGap(ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
										.addComponent(resetButton)))
				.addGap(310)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup().addGap(35)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel)
						.addComponent(teacherNameTextField, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE))
				.addGap(26)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_1)
						.addComponent(teacherSexManRadioButton).addComponent(teacherSexFemaleRadioButton))
				.addGap(37)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_2).addComponent(
						teacherTitleTextField, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
				.addGap(35)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_3)
						.addComponent(teacherAgeTextField, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
				.addGap(37)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_4)
						.addComponent(teacherPasswordField, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
				.addGap(35).addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(submitButton)
						.addComponent(resetButton))
				.addContainerGap()));
		getContentPane().setLayout(groupLayout);
	}

	protected void resetValue(ActionEvent ae) {
		// TODO 自动生成的方法存根
		teacherNameTextField.setText("");
		teacherTitleTextField.setText("");
		teacherAgeTextField.setText("");
		teacherPasswordField.setText("");
		teacherSexManRadioButton.setSelected(true);
	}

	protected void addTeacherAct(ActionEvent ae) {
		// TODO 自动生成的方法存根
		String teacherName = teacherNameTextField.getText().toString();
		String teacherSex = teacherSexManRadioButton.isSelected() ? teacherSexManRadioButton.getText().toString()
				: teacherSexFemaleRadioButton.getText().toString();
		String teacherTitle = teacherTitleTextField.getText().toString();
		String teacherPassword = String.valueOf(teacherPasswordField.getPassword());
		int teacherAge = 0;
		try {
			teacherAge = Integer.parseInt(teacherAgeTextField.getText().toString());
		} catch (Exception e) {
			// TODO: handle exception
			JOptionPane.showMessageDialog(this, "年龄只允许输入数字!");
			return;
		}
		if (StringUtil.isEmpty(teacherName)) {
			JOptionPane.showMessageDialog(this, "教师姓名必须填写!");
			return;
		}
		if (StringUtil.isEmpty(teacherTitle)) {
			JOptionPane.showMessageDialog(this, "教师职称必须填写!");
			return;
		}
		if (teacherAge <= 0) {
			JOptionPane.showMessageDialog(this, "教师年龄必须大于0!");
			return;
		}
		if (StringUtil.isEmpty(teacherPassword)) {
			JOptionPane.showMessageDialog(this, "教师登录密码必须填写!");
			return;
		}
		Teacher teacher = new Teacher();
		teacher.setName(teacherName);
		teacher.setSex(teacherSex);
		teacher.setTitle(teacherTitle);
		teacher.setAge(teacherAge);
		teacher.setPassword(teacherPassword);
		TeacherDao teacherDao = new TeacherDao();
		if (teacherDao.addTeacher(teacher)) {
			JOptionPane.showMessageDialog(this, "教师添加成功!");
		} else {
			JOptionPane.showMessageDialog(this, "教师添加失败!");
		}
		resetValue(ae);
	}
}
